package com.hnu.seckill.exception;

import com.hnu.seckill.vo.RespBean;
import com.hnu.seckill.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception exception) {
        if(exception instanceof GlobalException){
            GlobalException e = (GlobalException) exception;
            return RespBean.error(e.getRespBeanEnum());
        }
        else if (exception instanceof BindException){
            BindException e = (BindException) exception;
            RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);
            respBean.setMsg("参数校验异常："+e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }
}
